﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static Providers.LiveControl.Server.WindowApi;
namespace Providers.LiveControl.Server
{
    public class ScreenShotHelper
    {
        public Bitmap this[int index]
        {
            get
            {
                if (index > _screenShots.Count - 1)
                {
                    index = 0;
                }
                return _screenShots[index];
            }

        }

        private readonly List<Bitmap> _screenShots = new List<Bitmap>();

        public Rectangle GetRectangle(int index)
        {
            var allScreens = Screen.AllScreens;
            if (allScreens.Length-1 < index)
            {
                index = 0;
            }
            return allScreens[index].Bounds;
        }
      
        /// <summary>
        ///     获取屏幕截图
        ///     其中flagMirror为true时使用镜像截图，该截图有部分被透明，主要是弹窗内部
        ///     所以添加了屏幕截图功能，效率会比镜像低点，但好处是完整
        /// </summary>
        /// <returns></returns>
        public void ScreenShot()
        {
            var allScreens = Screen.AllScreens;
            foreach (var screenShot in _screenShots)
            {
                screenShot.Dispose();
            }
            _screenShots.Clear();
            foreach (var screen in allScreens)
            {
                Bitmap tempShot = CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, new Size(screen.Bounds.Width, screen.Bounds.Height));

                _screenShots.Add(tempShot);
            }
        }

        public Bitmap GetScreenshot()
        {
            Size sz = Screen.PrimaryScreen.Bounds.Size;
            IntPtr hDesk = GetDesktopWindow();
            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp);
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            return bmp;
            //bmp.Save(@"c:\temp\test.png");
            //bmp.Dispose();
        }

        public void ScreenShot(CopyPixelOperation copyPixelOperation)
        {
            
            var allScreens = Screen.AllScreens;
            foreach (var screenShot in _screenShots)
            {
                screenShot.Dispose();
            }
            _screenShots.Clear();
            foreach (var screen in allScreens)
            {
                Bitmap tempShot = CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, new Size(screen.Bounds.Width, screen.Bounds.Height));

                _screenShots.Add(tempShot);
            }
        }

        public bool FlagShotState = false;
        public Bitmap CopyFromScreen(int sourceX, int sourceY, int destinationX, int destinationY, Size blockRegionSize)
        {

            IntPtr hDesk = GetDesktopWindow();
            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, blockRegionSize.Width, blockRegionSize.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            FlagShotState = BitBlt(hDest, destinationX, destinationY, blockRegionSize.Width, blockRegionSize.Height, hSrce, sourceX, sourceY, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp);
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            return bmp;
        }
    }
}
